package com.njitzx.fileupload.controller;

import com.njitzx.fileupload.pojo.FileDTO;
import com.njitzx.fileupload.service.FileService;
import com.njitzx.fileupload.util.AjaxResult;
import com.njitzx.fileupload.util.MinioTemplate;
import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

@RestController

public class FileController {


    @Autowired
    private FileService fileService;

    @GetMapping("/hello")
    public AjaxResult uploadFileV1() {
        return AjaxResult.success("hello");
    }

    //单个文件上传
    @GetMapping("/uploadV1")
    public AjaxResult uploadFileV1(MultipartFile file) {
        Map<String, Object> map = fileService.uploadV1(file);
        return AjaxResult.success(map);
    }

    @PostMapping("/uploadV2")
    public AjaxResult uploadFileV2(
            @RequestParam("fileID") Long fileID,           // 接收文件ID
            @RequestParam("currentChunk") Integer currentChunk, // 接收当前块
            @RequestParam("totalChunk") Integer totalChunk,     // 接收总块数
            @RequestParam("file") MultipartFile file           // 接收文件数据
    ) throws IOException {
        fileService.uploadV2(fileID, currentChunk, totalChunk, file);
        return AjaxResult.success();
    }


    //去数据库中查询这个文件的哈希值是否存在,如果存在就不用上传了，直接拿到这个文件的地址
    @GetMapping("/md5")
    public AjaxResult getMd5(@RequestParam("md5") String md5) {
        Map<String, Object> map=fileService.getMd5(md5);
        return AjaxResult.success(map);
    }

    @GetMapping("/merge")
    public AjaxResult mergeFile(@RequestParam("fileID") Long fileID, @RequestParam("fileType") String fileType,
                                @RequestParam("fileName") String fileName) throws IOException {

        Map<String, Object> map = fileService.merge(fileID, fileType, fileName);
        return AjaxResult.success();
    }
}
